package me.sluijsens.Basket;

import java.io.File;
import java.io.IOException;
import java.util.logging.Logger;

import net.milkbowl.vault.economy.Economy;
import net.milkbowl.vault.permission.Permission;

import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.plugin.java.JavaPlugin;

public class Basket extends JavaPlugin {
	//define the listener variables
	private final BasketBlockListener blockListener = new BasketBlockListener(this);
	private final BasketPlayerListener playerListener = new BasketPlayerListener(this);
	
	//define the log variable
	Logger log = Logger.getLogger("Minecraft");
	
	public static Permission permission = null;
    public static Economy economy = null;

    private Boolean setupPermissions()
    {
        RegisteredServiceProvider<Permission> permissionProvider = getServer().getServicesManager().getRegistration(net.milkbowl.vault.permission.Permission.class);
        if (permissionProvider != null) {
            permission = permissionProvider.getProvider();
        }
        return (permission != null);
    }
    
    private Boolean setupEconomy()
    {
        RegisteredServiceProvider<Economy> economyProvider = getServer().getServicesManager().getRegistration(net.milkbowl.vault.economy.Economy.class);
        if (economyProvider != null) {
            economy = economyProvider.getProvider();
        }

        return (economy != null);
    }
	
	public void onEnable() {
		//declare pluginmanager, permissions and economy 
		PluginManager pm = this.getServer().getPluginManager();
		setupPermissions();
		setupEconomy();
		
		//handle events
		pm.registerEvents(blockListener, this);
		pm.registerEvents(playerListener, this);
		pm.registerEvents(playerListener, this);
		
		//Prepare configuration files
		File fileBaskets = new File(this.getDataFolder(), "baskets.yml");
		
		if(!fileBaskets.exists()) {
			FileConfiguration bConf = YamlConfiguration.loadConfiguration(fileBaskets);
			
			try {
				bConf.save(fileBaskets);
			} catch (IOException e) {
				log.info("Could not save baskets.yml!");
			}
		}
		
		log.info(this.getDescription().getName() + " v" + this.getDescription().getVersion() + " enabled!");
	}
	
	public void onDisable() {
		log.info(this.getDescription().getName() + " v" + this.getDescription().getVersion() + " disabled!");
	}
}
